using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Diagnostics;
using System.Text;

using UtilityLibrary.Win32;
using UtilityLibrary.General;


namespace UtilityLibrary.WinControls
{
	/// <summary>
	/// Summary description for TreeViewEx.
	/// </summary>
	[ToolboxItem(false)]
	public class TreeViewEx : System.Windows.Forms.TreeView
	{
		#region Class Variables
		// Helper to draw item state
		bool itemHasFocus = false;
		bool itemSelected = false;
		bool itemIsHot = false;
		bool itemClicked = false;
		IntPtr hClickedItem = IntPtr.Zero;
		#endregion

		#region Constructor
		public TreeViewEx()
		{
	
		}
		#endregion

		#region Overrides
		protected override  void WndProc(ref Message message)
		{
			base.WndProc(ref message);

			switch (message.Msg)
			{
				// Reflected Messages come from the treeview control itself
				case (int)ReflectedMessages.OCM_NOTIFY:
					NMHDR nm = (NMHDR) message.GetLParam(typeof(NMHDR));	
					switch (nm.code)
					{
						case (int)NotificationMessages.NM_CUSTOMDRAW:
							NotifyTreeCustomDraw(ref message); 
							break;
						case (int)TreeViewNotifications.TVN_ITEMEXPANDEDW:
							OnTreeViewItemExpanded(ref message);
							break;
						case (int)TreeViewNotifications.TVN_ITEMEXPANDINGW:
							OnTreeViewItemExpanding(ref message);
							break;
						case (int)TreeViewNotifications.TVN_SELCHANGINGW:
                            OnTreeViewSelectionChanging(ref message);
							break; 
                        default:
						break;
					}
					break;
				default:
					break;
			}

		}

		protected override void OnMouseDown(MouseEventArgs e)
		{
			base.OnMouseDown(e);
						
            TreeViewHitTestFlags flags; 
			hClickedItem = HitTest(new Point(e.X, e.Y), out flags);
			if ( (flags & TreeViewHitTestFlags.TVHT_ONITEMBUTTON) != 0 && e.Button != MouseButtons.Right )
				hClickedItem = IntPtr.Zero;
            	

		}
		#endregion

		#region Virtuals
		// Let derive class implement whatever functionality they wish
		protected virtual void OnTreeViewItemExpanded(ref Message m)
		{
			// This value is ignore
			m.Result = IntPtr.Zero;
		}

		protected virtual void OnTreeViewItemExpanding(ref Message m)
		{
			// Return zero to allow tree to send expand message
			m.Result = IntPtr.Zero;
		}

		protected virtual void OnTreeViewSelectionChanging(ref Message m)
		{
			m.Result = IntPtr.Zero;

		}
		#endregion

		#region Implementation
		bool NotifyTreeCustomDraw(ref Message m)
		{
			m.Result = (IntPtr)CustomDrawReturnFlags.CDRF_DODEFAULT;
			NMTVCUSTOMDRAW tvcd = (NMTVCUSTOMDRAW)m.GetLParam(typeof(NMTVCUSTOMDRAW));
			IntPtr thisHandle = Handle;
			
			if ( tvcd.nmcd.hdr.hwndFrom != Handle)
				return false;
			switch (tvcd.nmcd.dwDrawStage)
			{
				case (int)CustomDrawDrawStateFlags.CDDS_PREPAINT:
					// Ask for Item painting notifications
					m.Result = (IntPtr)CustomDrawReturnFlags.CDRF_NOTIFYITEMDRAW;
					break;
				case (int)CustomDrawDrawStateFlags.CDDS_ITEMPREPAINT:
					PrePaintCustomDrawing(ref m);
					break;
				case (int)CustomDrawDrawStateFlags.CDDS_ITEMPOSTPAINT:
					PostPaintCustomDrawing(ref m);
					break;
				default:
					break;

			}
			return false;
		}

		void PrePaintCustomDrawing(ref Message m)
		{
			NMTVCUSTOMDRAW tvcd = (NMTVCUSTOMDRAW)m.GetLParam(typeof(NMTVCUSTOMDRAW));
			itemHasFocus = false;
			itemSelected = false;
			itemIsHot = false;
			
			if(	(tvcd.nmcd.uItemState & (uint)CustomDrawItemStateFlags.CDIS_FOCUS) != 0 )
				itemHasFocus = true;

			if(	(tvcd.nmcd.uItemState & (uint)CustomDrawItemStateFlags.CDIS_SELECTED) != 0 )
				itemSelected = true;
			
			if(	(tvcd.nmcd.uItemState & (uint)CustomDrawItemStateFlags.CDIS_HOT) != 0 )
				itemIsHot = true;

			IntPtr hNode = (IntPtr)tvcd.nmcd.dwItemSpec;
			itemClicked = hClickedItem != IntPtr.Zero && hClickedItem == hNode;
            			
			if ( itemHasFocus || itemSelected || itemIsHot || itemClicked )
			{
				Color backColor = Color.Empty;
				Color foreColor = Color.Empty;

				if ( itemIsHot )
				{
					// Paint text and background in SystemColors.Window color
					// to make the painting less noticable, we will paint
					// this item in the postpaint notification
					foreColor = SystemColors.Window;
					backColor = SystemColors.Window;
				}
				else if ( itemHasFocus || itemSelected || itemClicked )
				{
					// We are just going to paint the border
					// in the postpaint notification
					// -- this helps a little bit to avoid flickering
					foreColor = SystemColors.ControlText;
					if ( itemSelected && !Focused && !HideSelection )
					{
						backColor = SystemColors.Control;
					}
					else
					{
						backColor = ColorUtil.VSNetSelectionColor;
					}
				}
				
				tvcd.clrText = ColorUtil.RGB(foreColor.R, foreColor.G, foreColor.B);
				tvcd.clrTextBk = ColorUtil.RGB(backColor.R,	backColor.G, backColor.B);
			
				// Put structure back in the message
				Marshal.StructureToPtr(tvcd, m.LParam, true);
				// Signal that we want item post paint notification
				m.Result = (IntPtr)CustomDrawReturnFlags.CDRF_NOTIFYPOSTPAINT;
			}
		}

		void PostPaintCustomDrawing(ref Message m)
		{
			NMTVCUSTOMDRAW tvcd = (NMTVCUSTOMDRAW)m.GetLParam(typeof(NMTVCUSTOMDRAW));
			IntPtr hNode = (IntPtr)tvcd.nmcd.dwItemSpec;
			Rectangle rect = GetItemRect(hNode);
			
			// Create a graphic object from the Device context in the message
			Graphics g = Graphics.FromHdc(tvcd.nmcd.hdc);
			
			if ( itemIsHot )
			{
				// Draw the whole item, background, text and border
				using ( Brush brush = new SolidBrush(ColorUtil.VSNetPressedColor))
				{
					g.FillRectangle(brush, rect.Left, rect.Top, rect.Width-1, rect.Height-1);
					g.DrawRectangle(SystemPens.Highlight, rect.Left, rect.Top, rect.Width-1, rect.Height-1);
				}
				
				// Draw Text
				string itemText = GetItemText(hNode);
				Size textSize = TextUtil.GetTextSize(g, itemText, Font);
				Point pos = new Point(rect.Left+2, rect.Top + (rect.Height - textSize.Height)/2);
				TextUtil.DrawText(g, itemText, Font, new Rectangle(pos, textSize));
			}
			else if ( itemHasFocus || itemSelected || itemClicked )
			{
				// Draw just the border
				g.DrawRectangle(SystemPens.Highlight, rect.Left, rect.Top, rect.Width-1, rect.Height-1);
				if ( itemClicked )
				{
					// Reset item clicked
					hClickedItem = IntPtr.Zero;
				}
			}

			// Put structure back in the message
			Marshal.StructureToPtr(tvcd, m.LParam, true);
			m.Result = 	(IntPtr)CustomDrawReturnFlags.CDRF_SKIPDEFAULT;

		}

		protected Rectangle GetItemRect(IntPtr hTreeItem)
		{
			RECT rc = new RECT();
			
			// This is how Microsoft recommends to shovel the handle to the tree Node into
			// the rectangle structure that will be used to send a message to retrieve
			// the bounds of the tree item. Any wonders why Java became a huge success?
			unsafe 
			{ 
				*(IntPtr*)&rc = hTreeItem;
			}
			
			// --I wanted to use the TreeView NET control itself to get the bounds of the current
			// tree node, but a quick inspection through the documentation made me realize that I would 
			// have to loop through the Nodes collection and all the subnodes collections that
			// each node have which would be terribly expensive just to retrieve such information,
			// specially when I need to do this really quick
			// instead let's use the horrible but efficient way--
			WindowsAPI.SendMessage(Handle, (int)TreeViewMessages.TVM_GETITEMRECT, 1, ref rc);
			
			return new Rectangle(rc.left, rc.top, rc.right-rc.left, rc.bottom-rc.top);
		}

		protected string GetItemText(IntPtr hTreeItem)
		{
			string text;
			TVITEM tvi = new TVITEM();
			tvi.hItem = hTreeItem;
			tvi.mask = (uint)TreeViewItemFlags.TVIF_TEXT;
			tvi.cchTextMax = 1024;
			tvi.pszText = Marshal.AllocHGlobal(1024);
			WindowsAPI.SendMessage(Handle, TreeViewMessages.TVM_GETITEMW, 0, ref tvi);
			text = Marshal.PtrToStringAuto(tvi.pszText);
			Marshal.FreeHGlobal(tvi.pszText);
			return text;
		}

		protected IntPtr HitTest(Point point, out TreeViewHitTestFlags flags)
		{
			// Initialize flags to nothing hit
			flags = TreeViewHitTestFlags.TVHT_NOWHERE;

			// Setup hittest structure
			TVHITTESTINFO hti = new TVHITTESTINFO();
            hti.pt.x = point.X;
			hti.pt.y = point.Y;
						            
			// Send message
			IntPtr handle = (IntPtr)WindowsAPI.SendMessage(Handle, TreeViewMessages.TVM_HITTEST, 0, ref hti);
			if ( handle != IntPtr.Zero )
                flags = hti.flags;
			
			return handle;
		}
		#endregion
		
	}
}
